package com.aaa.sys.controller;

import com.aaa.sys.entity.Menu;
import com.aaa.sys.entity.TreeNode;
import com.aaa.sys.service.MenuService;
import com.aaa.sys.util.JwtUtils;
import com.auth0.jwt.interfaces.Claim;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import com.aaa.common.util.BaseController;
import com.aaa.common.util.Result;
import com.aaa.common.vo.Page;

import java.util.List;
import java.util.Map;


/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2023-06-20 09:08:44
 */
@RestController
@RequestMapping("menu")
@Log4j2
public class MenuController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    @GetMapping("queryChildrenNumById")
    public Result queryChildrenNumById(Integer id){
        Integer childrenNum = menuService.queryChildrenNumById(id);
        System.out.println("childrenNum---------------"+childrenNum);
        return success(childrenNum);
    }
    /**
     * 查询所有的树数据
     * @return
     */
    @GetMapping("queryAllTreeData")
    public Result queryAllTreeData(){
        List<TreeNode> treeNodes = menuService.queryAllTree();
        System.out.println("treeNodes---------------------"+treeNodes);
        return success(treeNodes);
    }

    @GetMapping("queryTreeDataByUserId")
    public Result queryTreeDataByUserId(String token){
        String tokenSub = token.substring(token.indexOf("-") + 1);
        Map<String, Claim> payloadByToken = JwtUtils.getPayloadByToken(tokenSub);
        Claim userId1 = payloadByToken.get("userId");
        log.info(userId1+".................");
        //userId="1"
        String userId = userId1.toString().replace("\"", "");
        //根据用户编号，查询出该用户权限菜单
        return success(menuService.queryTreeDataByUserId(Integer.valueOf(userId)));
    }


    /**
     * 获取当前用户拥有权限按钮列表
     * @param token
     * @return
     */
    @GetMapping("queryButtonPermByToken")
    public Result queryButtonPermByToken(String token){
        String tokenSub = token.substring(token.indexOf("-") + 1);
        Map<String, Claim> payloadByToken = JwtUtils.getPayloadByToken(tokenSub);
        Claim userId1 = payloadByToken.get("userId");
        log.info(userId1+".................");
        //userId="1"
        String userId = userId1.toString().replace("\"", "");
        return success(menuService.queryButtonPermsByUserId(Integer.valueOf(userId)));
    }

    /**
     * 分页查询
     *
     * @param page 分页查询封装
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Menu> page) {
        return success(this.menuService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer id) {
        return success(this.menuService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param menu 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Menu menu) {
        return success(this.menuService.insert(menu));
    }

    /**
     * 编辑数据
     *
     * @param menu 实体
     * @return 编辑结果
     */
    @PostMapping("update")
    public Result update(@RequestBody Menu menu) {
        return success(this.menuService.update(menu));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return success(this.menuService.deleteById(id));
    }

}

